package com.test.prolenders.activity_prolenders.ln_prolenders;

import static com.test.prolenders.config_prolenders.UserState_prolenders.*;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.test.prolenders.R;
import com.test.prolenders.base_prolenders.BaseActivity_prolenders;
import com.test.prolenders.base_prolenders.BaseLnFragment_prolenders;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_prolenders;
import com.test.prolenders.config_prolenders.UserState_prolenders;
import com.test.prolenders.fragment_prolenders.ln_state_prolenders.LnRpyFragment_prolenders;
import com.test.prolenders.fragment_prolenders.ln_state_prolenders.LnStateFailFragment_prolenders;
import com.test.prolenders.fragment_prolenders.ln_state_prolenders.LnStateWaitFragment_prolenders;
import com.test.prolenders.fragment_prolenders.main_prolenders.AgreementFragment_prolenders;
import com.test.prolenders.fragment_prolenders.main_prolenders.HomeFragment_prolenders;
import com.test.prolenders.fragment_prolenders.main_prolenders.PersonalCenterFragment_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity_prolenders extends BaseActivity_prolenders {

    private View homeView, pcView,dsView;
    private ImageView homeIv, pcIv,dsIv;
    private TextView homeTv, pcTv,dsTv;

    private BaseLnFragment_prolenders baseLnFragment;
    private PersonalCenterFragment_prolenders pcFragment;
    private AgreementFragment_prolenders dsFragment;

    private int pageFlag = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_prolenders);

        page = "MAIN";

        homeView = findViewById(R.id.am_view_home);
        pcView = findViewById(R.id.am_view_me);
        dsView = findViewById(R.id.am_view_ds);
        homeIv = findViewById(R.id.am_iv_home);
        pcIv = findViewById(R.id.am_iv_me);
        dsIv = findViewById(R.id.am_iv_ds);
        homeTv = findViewById(R.id.am_tv_home);
        pcTv = findViewById(R.id.am_tv_me);
        dsTv = findViewById(R.id.am_tv_ds);

        init();
    }

    private void init(){

        pcFragment = new PersonalCenterFragment_prolenders();
        dsFragment = new AgreementFragment_prolenders();

        homeView.setOnClickListener(v -> toHome());
        pcView.setOnClickListener(v -> toPc());
        dsView.setOnClickListener(v -> toDs());

        //初始化用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.am_layout_fragment,pcFragment).add(R.id.am_layout_fragment,dsFragment).commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUserState();
    }

    /**
     * 判断用户流程状态，如果状态有改变再更新页面
     */
    private void updateUserState(){
        //判断用户流程状态
        String state = ProcessInfo_prolenders.getInstance().getUserState();
        if (UserState_prolenders.isStateChange(state)) updatePageState(state);
    }

    private void updatePageState(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (baseLnFragment != null) fragmentTransaction.remove(baseLnFragment);

        switch (userState){
            case State_Rp:
                baseLnFragment = new LnRpyFragment_prolenders();
                break;
            case State_Wait:
                baseLnFragment = new LnStateWaitFragment_prolenders();
                break;
            case State_Fail:
                baseLnFragment = new LnStateFailFragment_prolenders();
                break;
            default:
                baseLnFragment = new HomeFragment_prolenders();
        }

        fragmentTransaction.add(R.id.am_layout_fragment, baseLnFragment);

        switch (pageFlag) {
            case 2:
                fragmentTransaction.show(pcFragment).hide(baseLnFragment).hide(dsFragment);
                break;
            case 3:
                fragmentTransaction.show(dsFragment).hide(baseLnFragment).hide(pcFragment);
                break;
            default:
                fragmentTransaction.show(baseLnFragment).hide(pcFragment).hide(dsFragment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }


    private void toHome(){

        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"HOME_CLICK","");

        pageFlag = 1;

        homeIv.setBackgroundResource(R.drawable.icon_home_true_prolenders);
        pcIv.setBackgroundResource(R.drawable.icon_pc_false_prolenders);
        dsIv.setBackgroundResource(R.drawable.icon_ds_false_prolenders);

        homeTv.setTextColor(getColor(R.color.tab_text_true_prolenders));
        pcTv.setTextColor(getColor(R.color.tab_text_false_prolenders));
        dsTv.setTextColor(getColor(R.color.tab_text_false_prolenders));

        getSupportFragmentManager().beginTransaction().show(baseLnFragment).hide(pcFragment).hide(dsFragment).commitAllowingStateLoss();

    }

    private void toPc(){

        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"ME_CLICK","");

        pageFlag = 2;

        homeIv.setBackgroundResource(R.drawable.icon_home_false_prolenders);
        pcIv.setBackgroundResource(R.drawable.icon_pc_true_prolenders);
        dsIv.setBackgroundResource(R.drawable.icon_ds_false_prolenders);

        homeTv.setTextColor(getColor(R.color.tab_text_false_prolenders));
        pcTv.setTextColor(getColor(R.color.tab_text_true_prolenders));
        dsTv.setTextColor(getColor(R.color.tab_text_false_prolenders));

        getSupportFragmentManager().beginTransaction().show(pcFragment).hide(baseLnFragment).hide(dsFragment).commitAllowingStateLoss();

    }

    private void toDs(){

        HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"STATEMENT_CLICK","");

        pageFlag = 3;

        homeIv.setBackgroundResource(R.drawable.icon_home_false_prolenders);
        pcIv.setBackgroundResource(R.drawable.icon_pc_false_prolenders);
        dsIv.setBackgroundResource(R.drawable.icon_ds_true_prolenders);

        homeTv.setTextColor(getColor(R.color.tab_text_false_prolenders));
        pcTv.setTextColor(getColor(R.color.tab_text_false_prolenders));
        dsTv.setTextColor(getColor(R.color.tab_text_true_prolenders));

        getSupportFragmentManager().beginTransaction().show(dsFragment).hide(pcFragment).hide(baseLnFragment).commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserState_prolenders.clearState();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (baseLnFragment!=null && baseLnFragment instanceof HomeFragment_prolenders) EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,baseLnFragment);
    }
}